<!DOCTYPE html>
<html lang="zh">

<head>
  <link rel="stylesheet" href="css/prism.css">
  <style>
    html,
    body {
      box-sizing: border-box;
      margin: 0;
      padding: 0;
      height: 100%;
      font-size: 12px;
    }

    body {
      min-height: 500px;
    }

    section {
      display: flex;
      flex-wrap: wrap;
    }

    .code {
      margin-top: 3px;
    }

    pre[class*=language-] {
      margin: 0;
      padding: 0;
    }

    main {
      border-top: 2px solid #ccc;
      width: 100%;
      height: 35%;
      min-height: 200px;
    }
  </style>
  <title>冒泡排序_改进2</title>
</head>

<body>
  <section class="frames"></section>
  <div class="code">
    <pre><code class="language-java">public static void bubble_v2(int[] a) {
  int n = a.length - 1;
  while (true) {
    int last = 0; // 表示最后一次交换索引位置
    for (int i = 0; i < n; i++) {
      if (a[i] > a[i + 1]) {
        swap(a, i, i + 1);
        last = i;
      }
    }
    n = last;
    if (n == 0) {
      break;
    }
  }
}</code></pre>
  </div>
  <main></main>
  <section>
    <div style='background-color:#67cdcc; margin: 2px 2px 0 0; padding: 4px 6px;'>未排序区域</div>
    <div style='background-color:#ccc; margin: 2px 2px 0 0; padding: 4px 6px;'>已排序区域</div>
    <div style='background-color:#cc99cd; margin: 2px 2px 0 0; padding: 4px 6px;'>索引</div>
    <div style='background-color:#f08d49; margin: 2px 2px 0 0; padding: 4px 6px;'>要交换</div>
  </section>
  <div style='margin: 2px 2px 0 0; padding: 4px 6px;'>
    <span>初始数组&nbsp;</span><input type="text" id='data' class="saveable" value="3,2,4,1,6,5,7">
    <span>动画速度(ms)&nbsp;</span><input type="number" step="100" value="300" id="animate_speed" class="saveable">
    <input style='font-size:12px;' type="button" value="保存" onclick="onSave('sort_bubble3')">
  </div>
  <script src="js/p5.js"></script>
  <script src="js/p5-svg.js"></script>
  <script src="js/util.js"></script>
  <script src="js/prism.js"></script>
  <script>
    const options = loadOptionsFromStorage('sort_bubble3')
    const RECT_WIDTH = 30                 // 矩形宽度、圆直径
    const SPACING = 1                     // 间隙
    const INDEX_RECT_HEIGHT = 20          // 索引矩形高度
    const POINTER_HEIGHT = 150            // 指针高度, 从底部算
    const VALUE_RECT_MIN_HEIGHT = 20      // 值矩形最小高度
    const VALUE_RECT_MAX_HEIGHT = 100     // 值矩形最大高度
    const d = new Draw(options.animate_speed)
    let data = options.data.split(',').map(e => Number(e))   
    let heightMap = new Map()
    function preload() {
      // const font = loadFont('JetBrainsMono-Regular.ttf')
    }
    function setup() {
      const WIN_WIDTH = document.querySelector('main').clientWidth
      const WIN_HEIGHT = document.querySelector('main').clientHeight
      const FONT_SIZE = 10
      createCanvas(WIN_WIDTH, WIN_HEIGHT, SVG)
      textSize(FONT_SIZE)
      textAlign(CENTER)
      heightMap = calculateRectHeight(data, VALUE_RECT_MIN_HEIGHT, VALUE_RECT_MAX_HEIGHT)
      bubble(data)
      d.updateFrameButtons()
    }
    function draw() {
      d.draw(() => background('#2d2d2d'))
    }
    /*
      array: 数组
      highlights: 高亮位置
      pointers: 指针
      unsorted: 未排序个数
      lineNumber: 高亮行号
    */
    function frame({ array, pointers, highlights, lineNumber, unsorted }) {
      const pre = document.querySelector('pre')
      if (lineNumber > 0) {
        pre.setAttribute('data-line', lineNumber)
        Prism.highlightAllUnder(pre)
      }
      const LEFT = (width - array.length * (RECT_WIDTH + SPACING)) / 2
      for (let i = 0; i < array.length; i++) {
        // 注：矩形以左下角 x, y 作为起点坐标
        let x = LEFT + i * (RECT_WIDTH + SPACING)
        let y = height - (SPACING + INDEX_RECT_HEIGHT)
        stroke(255)
        pointers.draw(i, x + RECT_WIDTH / 2, INDEX_RECT_HEIGHT + SPACING * 2)
        if (i > unsorted) {
          fill('#ccc')
        } else {
          highlights.includes(i) ? fill('#f08d49') : fill('#67cdcc')
        }
        noStroke()
        rect(x, y, RECT_WIDTH, -heightMap.get(array[i]))
        fill('#ffffff')
        text(array[i], x + RECT_WIDTH / 2, y - 6)
        fill('#cc99cd')
        rect(x, height, RECT_WIDTH, -INDEX_RECT_HEIGHT)
        fill('#ffffff')
        text(i, x + RECT_WIDTH / 2, height - 6)
      }
    }
    function drawPointer(x, y, i) {
      line(x + RECT_WIDTH / 2, height - INDEX_RECT_HEIGHT - SPACING * 2, x + RECT_WIDTH / 2, height - POINTER_HEIGHT)
      circle(x + RECT_WIDTH / 2, height - POINTER_HEIGHT, RECT_WIDTH)
      fill(0)
      text(i, x + RECT_WIDTH / 2, height - POINTER_HEIGHT + 4)
    }
    
    function bubble(a) {
      let n = a.length - 1
      d.add({ array: a, unsorted: n }, frame)
      while (true) {
        let last = 0
        for (let i = 0; i < n; i++) {
          d.add({ array: a, pointers: [{ index: i, text: 'i' }, { index: i + 1, text: 'i+1' }, { index: last, text: 'x' }], unsorted: n, lineNumber: 6 }, frame)
          if (a[i] > a[i + 1]) {
            d.add({ array: a, pointers: [{ index: i, text: 'i' }, { index: i + 1, text: 'i+1' }, { index: last, text: 'x' }], highlights: [i, i + 1], unsorted: n, lineNumber: 7 }, frame)
            swap(a, i, i + 1)
            d.add({ array: a, pointers: [{ index: i, text: 'i' }, { index: i + 1, text: 'i+1' }, { index: last, text: 'x' }], highlights: [i, i + 1], unsorted: n, lineNumber: 8 }, frame)
            last = i
          }
        }
        n = last
        d.add({ array: a, unsorted: n, lineNumber: 11, keyframe:true }, frame)
        if (n == 0) {
          d.add({ array: a, unsorted: n, lineNumber: 12 }, frame)
          d.add({ array: a, unsorted: -1, lineNumber: 13 }, frame)
          break
        }
      }
      d.add({ array: a, unsorted: -1 }, frame)
    }
    function swap(a, i, j) {
      let k = a[i]
      a[i] = a[j]
      a[j] = k
    }
  </script>
</body>

</html>